home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / ARGONET / PD / DTP / PDFENCRY.SPK / encryption_d next >
Text File  |  1998-04-13  |  10KB  |  365 lines

  1. diff ../xpdf-0.7a/xpdf/Makefile xpdf/Makefile
  2. 32c32,33
  3. <     XOutputDev.o XRef.o xpdf.o
  4. ---
  5. >     XOutputDev.o XRef.o xpdf.o md5.o rc4.o
  6. 46c47,48
  7. <     Parser.o PDFDoc.o PSOutputDev.o Stream.o XRef.o pdftops.o
  8. ---
  9. >     Parser.o PDFDoc.o PSOutputDev.o Stream.o XRef.o pdftops.o md5.o rc4.o
  10. 57c59,60
  11. <     Parser.o PDFDoc.o TextOutputDev.o Stream.o XRef.o pdftotext.o
  12. ---
  13. >     Parser.o PDFDoc.o TextOutputDev.o Stream.o XRef.o pdftotext.o md5.o rc4.o
  14. diff ../xpdf-0.7a/xpdf/Parser.cc xpdf/Parser.cc
  15. 18a19,20
  16. > #include "md5.h"
  17. > #include "XRef.h"
  18. 160a163,198
  19. > }
  20. > Object *Parser::getEncryptedObj(Object *obj, int num, int gen) {
  21. >   Stream *str1, *str2;
  22. >   MD5 context;
  23. >   RC4KEY rc4Key;
  24. >   GString MD5Key;
  25. >   GString Key;
  26. >   getObj(obj);
  27. >   // check for obj type
  28. >   if (obj->isStream()) {
  29. >     // enable Decryption on base File Stream
  30. >     str2 = obj->getStream();
  31. >     do {
  32. >       str1 = str2;
  33. >       str2 = str1->getBaseStream();
  34. >     } while (str1 != str2);
  35. >     Key.append(xref->getEncryptionKey());
  36. >     Key.append((char)( num       & 0xff));
  37. >     Key.append((char)((num >> 8) & 0xff));
  38. >     Key.append((char)((num >>16) & 0xff));
  39. >     Key.append((char)( gen       & 0xff));
  40. >     Key.append((char)((gen >> 8) & 0xff));
  41. >     context.update((unsigned char *)Key.getCString(), 10);
  42. >     context.finalize();
  43. >     MD5Key.clear();
  44. >     MD5Key.append((char *)context.raw_digest(), 10);
  45. >     // determine rc4Key
  46. >     rc4ExpandKey(&rc4Key, (unsigned char *)MD5Key.getCString(), 10);
  47. >     str1->enableEncryption(&rc4Key);
  48. >   }
  49. >   return obj;
  50. diff ../xpdf-0.7a/xpdf/Parser.h xpdf/Parser.h
  51. 32a33
  52. >   Object *getEncryptedObj(Object *obj, int num, int gen);
  53. diff ../xpdf-0.7a/xpdf/Stream.cc xpdf/Stream.cc
  54. 217a218
  55. >   encryption = gFalse;
  56. 245a247,249
  57. >   if (encryption == gTrue) {
  58. >     rc4Crypt(&rc4Key, (unsigned char *)buf, n);
  59. >   }
  60. 294a299,307
  61. > void FileStream::enableEncryption(RC4KEY *prc4Key)
  62. > {
  63. >   // prepary RC4 key
  64. >   rc4Key = *prc4Key;
  65. >   encryption = gTrue;
  66. > }
  67. diff ../xpdf-0.7a/xpdf/Stream.h xpdf/Stream.h
  68. 17a18
  69. > #include <Error.h>
  70. 20a22,25
  71. > //extern "C" {
  72. > #include "rc4.h"
  73. > //}
  74. 77a83,84
  75. >   virtual void enableEncryption(RC4KEY *rc4Key) {error(-1, "No FileStream, no decryption");};
  76. 112a120,121
  77. >   void enableEncryption(RC4KEY *rc4Key);
  78. 113a123,124
  79. >   RC4KEY rc4Key;
  80. >   GBool encryption;
  81. diff ../xpdf-0.7a/xpdf/XRef.cc xpdf/XRef.cc
  82. 24a25,28
  83. > #include "md5.h"
  84. > //extern "C" {
  85. > #include "rc4.h"
  86. > //}
  87. 48a53
  88. >   encrypted = gFalse;
  89. 93c98,102
  90. <   // check for encryption
  91. ---
  92. >   m_okToPrint = gTrue;
  93. >   m_okToCopy = gTrue;
  94. >   m_okToChange = gTrue;
  95. >   m_okToAddNotes = gTrue;
  96. 95,97c104,108
  97. <     ok = gFalse;
  98. <     xref = oldXref;
  99. <     return;
  100. ---
  101. >     if (setupDecryption() == gFalse) {
  102. >       ok = gFalse;
  103. >       xref = oldXref;
  104. >       return;
  105. >     }
  106. 103a115
  107. >   encryptionDict.free();
  108. 302a315
  109. 383,393c396,397
  110. <   Object obj;
  111. <   GBool encrypted;
  112. <   trailerDict.dictLookup("Encrypt", &obj);
  113. <   if ((encrypted = !obj.isNull())) {
  114. <     error(-1, "PDF file is encrypted and cannot be displayed");
  115. <     error(-1, "* Decryption support is currently not included in xpdf");
  116. <     error(-1, "* due to legal restrictions: the U.S.A. still has bogus");
  117. <     error(-1, "* export controls on cryptography software.");
  118. <   }
  119. <   obj.free();
  120. ---
  121. >   trailerDict.dictLookup("Encrypt", &encryptionDict);
  122. >   encrypted = encryptionDict.isDict();
  123. 398c402
  124. <   return gTrue;
  125. ---
  126. >   return m_okToPrint;
  127. 402c406,414
  128. <   return gTrue;
  129. ---
  130. >   return m_okToCopy;
  131. > }
  132. > GBool XRef::okToChange() {
  133. >   return m_okToChange;
  134. > }
  135. > GBool XRef::okToAddNotes() {
  136. >   return m_okToAddNotes;
  137. 427c439,443
  138. <       parser->getObj(obj);
  139. ---
  140. >       if (encrypted) {
  141. >         parser->getEncryptedObj(obj, num, gen);
  142. >       } else {
  143. >         parser->getObj(obj);
  144. >       }
  145. 438a455,614
  146. > }
  147. > GBool XRef::setupDecryption() {
  148. >   Object obj;
  149. >   GBool encrypted;
  150. >   GBool passwordOk;
  151. >   GString userPassword;
  152. >   // check filter
  153. >   encryptionDict.dictLookupNF("Filter", &obj);
  154. >   if (!obj.isName()) {
  155. >     error(-1, "No filter specified, asume Standard filter");
  156. >   } else if (strcmp(obj.getName(), "Standard") != 0) {
  157. >     error(-1, "File is encrypted with a non Standard Filter.\
  158. > xpdf supports only the standard encryption filter");
  159. >     obj.free();
  160. >     return gFalse;
  161. >   }
  162. >   obj.free();
  163. >   // check for no user password
  164. >   userPassword.clear();
  165. >   if (checkUserPassword(&userPassword) == gFalse) {
  166. >     // ask for user password
  167. >     // ...
  168. >     // check with user password
  169. >     error(-1, "pdf file is encrypted with a user password.\
  170. > xpdf does not support passwords please contact the author of the pdf file\
  171. > and ask him for a non password encrypted version");
  172. >     return gFalse;
  173. >   }
  174. >   // set permissions
  175. >   // permissions
  176. >   encryptionDict.dictLookupNF("P", &obj);
  177. >   if (obj.isNull() || !obj.isInt()) {
  178. >     error(-1, "No permissions specified");
  179. >     return gFalse;
  180. >   }
  181. >   int Permissions = obj.getInt();
  182. >   obj.free();
  183. >   m_okToPrint = Permissions & 0x04 ? gTrue : gFalse;
  184. >   m_okToChange = Permissions & 0x08 ? gTrue : gFalse;
  185. >   m_okToCopy = Permissions & 0x10 ? gTrue : gFalse;
  186. >   m_okToAddNotes = Permissions & 0x20 ? gTrue : gFalse;
  187. >   return gTrue;
  188. > }
  189. > GBool
  190. > XRef::checkUserPassword(GString *userPassword)
  191. > {
  192. >   GString preparedPassword;
  193. >   RC4KEY rc4Key;
  194. >   Object obj;
  195. >   char localPassword[32];
  196. >   if (preparePassword(userPassword, &preparedPassword) == gFalse) {
  197. >     return gFalse;
  198. >   }
  199. >   if (MakeEncryptionKey(&preparedPassword, &encryptionKey) == gFalse) {
  200. >     return gFalse;
  201. >   }
  202. >   // get the User password
  203. >   encryptionDict.dictLookupNF("U", &obj);
  204. >   if (obj.isNull() || !obj.isString()) {
  205. >     error(-1, "No user password specified");
  206. >     return gFalse;
  207. >   }
  208. >   memcpy(localPassword, obj.getString()->getCString(), 32);
  209. >   obj.free();
  210. >   // prepary RC4 key
  211. >   rc4ExpandKey(&rc4Key, (unsigned char *)encryptionKey.getCString(), 5);
  212. >   // encrypt User password
  213. >   rc4Crypt(&rc4Key, (unsigned char *)&localPassword[0], 32);
  214. >   // compare preparedPassword and decryptedPassword
  215. >   for (int i=0; i<32; i++) {
  216. >     if (localPassword[i] != preparedPassword.getChar(i)) {
  217. >       // not equal
  218. >       return gFalse;
  219. >     }
  220. >   }
  221. >   // password OK
  222. >   return gTrue;
  223. > }
  224. > GBool
  225. > XRef::MakeEncryptionKey(GString *password, GString *encryptionKey)
  226. > {
  227. >   MD5 context;
  228. >   Object obj, obj1;
  229. >   unsigned char Pkey[4];
  230. >   context.update((unsigned char *)password->getCString(), 32);
  231. >   // others
  232. >   // owner key
  233. >   encryptionDict.dictLookupNF("O", &obj);
  234. >   if (obj.isNull() || !obj.isString()) {
  235. >     error(-1, "No owner password specified");
  236. >     return gFalse;
  237. >   }
  238. >   context.update((unsigned char *)obj.getString()->getCString(), 32);
  239. >   obj.free();
  240. >   // permissions
  241. >   encryptionDict.dictLookupNF("P", &obj);
  242. >   if (obj.isNull() || !obj.isInt()) {
  243. >     error(-1, "No permissions specified");
  244. >     return gFalse;
  245. >   }
  246. >   int Permissions = obj.getInt();
  247. >   Pkey[0] =  Permissions        & 0xff;
  248. >   Pkey[1] = (Permissions >> 8 ) & 0xff;
  249. >   Pkey[2] = (Permissions >> 16) & 0xff;
  250. >   Pkey[3] = (Permissions >> 24) & 0xff;
  251. >   context.update(&Pkey[0], 4);
  252. >   obj.free();
  253. >   // first element from ID
  254. >   trailerDict.dictLookupNF("ID", &obj);
  255. >   if (obj.isNull() || !obj.isArray()) {
  256. >     error(-1, "No ID specified");
  257. >     return gFalse;
  258. >   }
  259. >   obj.arrayGet(0, &obj1);
  260. >   if (obj1.isNull() || !obj1.isString()) {
  261. >     error(-1, "No ID elements specified");
  262. >     return gFalse;
  263. >   }
  264. >   context.update((unsigned char *)(obj1.getString()->getCString()), (int)(obj1.getString()->getLength()));
  265. >   obj1.free();
  266. >   obj.free();
  267. >   context.finalize();
  268. >   encryptionKey->clear();
  269. >   encryptionKey->append((char *)context.raw_digest(), 5);
  270. >   return gTrue;
  271. > }
  272. > GBool
  273. > XRef::preparePassword(GString *password, GString *preparedPassword)
  274. > {
  275. >   static char acFill[32] = {0x28, 0xbf, 0x4e, 0x5e, 0x4e, 0x75, 0x8a, 0x41,
  276. >                         0x64, 0x00, 0x4e, 0x56, 0xff, 0xfa, 0x01, 0x08,
  277. >                         0x2e, 0x2e, 0x00, 0xb6, 0xd0, 0x68, 0x3e, 0x80,
  278. >                         0x2f, 0x0c, 0xa9, 0xfe, 0x64, 0x53, 0x69, 0x7a};
  279. >   preparedPassword->clear();
  280. >   preparedPassword->append(password);
  281. >   preparedPassword->append(&acFill[0], 32);
  282. >   return gTrue;
  283. diff ../xpdf-0.7a/xpdf/XRef.h xpdf/XRef.h
  284. 47a48,49
  285. >   GBool okToChange();
  286. >   GBool okToAddNotes();
  287. 54a57,59
  288. >   // get encryption key
  289. >   GString *getEncryptionKey() { return &encryptionKey; };
  290. 69a75,87
  291. >   GBool m_okToPrint;
  292. >   GBool m_okToCopy;
  293. >   GBool m_okToChange;
  294. >   GBool m_okToAddNotes;
  295. >   Object encryptionDict;        // encryption dictionary
  296. >   GBool encrypted;
  297. >   GString encryptionKey;
  298. >   GBool setupDecryption();
  299. >   GBool checkUserPassword(GString *userPassword);
  300. >   GBool MakeEncryptionKey(GString *password, GString *encryptionKey);
  301. >   GBool preparePassword(GString *password, GString *preparedPassword);
  302. diff ../xpdf-0.7a/xpdf/config.h xpdf/config.h
  303. 17c17
  304. < #define xpdfVersion "0.7a"
  305. ---
  306. > #define xpdfVersion "0.7a (encryption)"
  307. 24a25
  308. > #define xpdfCopyright_Encryption "Encryption by Leo J.B. Smiers"
  309. diff ../xpdf-0.7a/xpdf/pdftops.cc xpdf/pdftops.cc
  310. 63a64
  311. >     fprintf(stderr, "%s\n", xpdfCopyright_Encryption);
  312. diff ../xpdf-0.7a/xpdf/pdftotext.cc xpdf/pdftotext.cc
  313. 61a62
  314. >     fprintf(stderr, "%s\n", xpdfCopyright_Encryption);
  315. diff ../xpdf-0.7a/xpdf/xpdf.cc xpdf/xpdf.cc
  316. 315a316
  317. >     fprintf(stderr, "%s\n", xpdfCopyright_Encryption);
  318. 355a357
  319. >   fprintf(stderr, "%s\n", xpdfCopyright_Encryption);
  320.